/*
 * Figurka.java
 *
 * Created on 18. �erven 2007, 19:42
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package cz.possoft.serializable.chess.figurky;

import java.io.Serializable;

import cz.possoft.serializable.chess.game.ChessPole;


/**
 *
 * @author mara
 */
public abstract class Figurka implements Serializable {
	
	public static final long serialVersionUID = 734898945;		
    
    // aktualni souradnice dane figurky. Moznosti jsou 0...7
    private int x;
    private int y;

    // barva dane figurky
    // 1 - cerna, 2 - bila, 0 - prazdne pole
    private int color;

    // je figurka oznacena pro tah?
    private boolean markedForPlay = false;

    // je figurka oznacena prstencem (je nad ni mys)?
    private boolean ring = false;

    // sachovnice na ktere je dana figurka
    private ChessPole sachovnice;
    
    // true pokud figurka uz odehrala tah. Ma smysl jen u krale a veze kvuli rosade.
    private boolean odehralTah = false;

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public int getColor() {
        return color;
    }

    public void setColor(int color) {
        this.color = color;
    }

    public boolean isMarkedForPlay() {
        return markedForPlay;
    }

    public void setMarked(boolean marked) {
        this.markedForPlay = marked;
    }

    public boolean isRing() {
        return ring;
    }

    public void setRing(boolean ring) {
        this.ring = ring;
    }
    
    public boolean isOdehralTah() {
        return odehralTah;
    }

    public void setOdehralTah(boolean odehralTah) {
        this.odehralTah = odehralTah;
    }       

    // vraci true jestli muzu tahnout na dane pole (nebere se v uvahu vystaveni krale do sachu
    public abstract boolean muzeTahnout2(int xCil, int yCil);
    
    // vraci nazev figurky
    public abstract String getFigurkaName();            

    // vraci true jestli muze tahnout a pritom nevystavi vlastniho krale do sachu
    public boolean muzeTahnout(int xCil, int yCil) {
        if (!muzeTahnout2(xCil, yCil)) {
            return false;
        }
        if (!nebudeSach(xCil, yCil)) {
            return false;
        }
        return true;
    }
    
    public boolean nebudeSach(int xCil, int yCil) {
        // prozatimni prenastaveni sachovnice
        boolean b = true;
        int xpom = x;
        int ypom = y;
        // pokud je toto kral, tak se musi prenastavit i jeho lokace
        if (this instanceof King) {
            if (color == 1) {
                   getSachovnice().getBlackKingLocation().setX(xCil);
                   getSachovnice().getBlackKingLocation().setY(yCil);
            }
            else {
                getSachovnice().getWhiteKingLocation().setX(xCil);
                getSachovnice().getWhiteKingLocation().setY(yCil);
            }
        }
        Figurka puvodni = getSachovnice().getFigurka(xCil, yCil);
        getSachovnice().setFigurka(xCil, yCil, this);
        x = xCil;
        y = yCil;
        getSachovnice().setFigurka(xpom, ypom, new Prazdna(xpom, ypom, getSachovnice()));
        
        // vymena figurek dokoncena. ptam se na sach
        if (getColor() == 1) {
            if (getSachovnice().isSach(1, getSachovnice().getBlackKingLocation().getX(), getSachovnice().getBlackKingLocation().getY())) {
                b = false;
            }
        } else if (getColor() == 2) {
            if (getSachovnice().isSach(2, getSachovnice().getWhiteKingLocation().getX(), getSachovnice().getWhiteKingLocation().getY())) {
                b = false;
            }
        }
        // prenastaveni sachovnice zpet
        getSachovnice().setFigurka(xpom, ypom, this);
        x = xpom;
        y = ypom;
        getSachovnice().setFigurka(xCil, yCil, puvodni);
        if (this instanceof King) {
            if (color == 1) {
                   getSachovnice().getBlackKingLocation().setX(x);
                   getSachovnice().getBlackKingLocation().setY(y);
            }
            else {
                getSachovnice().getWhiteKingLocation().setX(x);
                getSachovnice().getWhiteKingLocation().setY(y);
            }
        }
        return b;    
    }

    public ChessPole getSachovnice() {
        return sachovnice;
    }

    public void setSachovnice(ChessPole sachovnice) {
        this.sachovnice = sachovnice;
    }

    // vraci true pokud je volna cesta k cili - nestoji na ni zadne jine figurky
    public boolean freeWay(int x2, int y2) {        
        int xzm;
        int yzm;
        int x1 = getX();
        int y1 = getY();
        if (x1 == x2) {
            xzm = 0;
        } else if (x1 > x2) {
            xzm = -1;
        } else {
            xzm = 1;
        }
        if (y1 == y2) {
            yzm = 0;
        } else if (y1 > y2) {
            yzm = -1;
        } else {
            yzm = 1;
        }
        x1 += xzm;
        y1 += yzm;
        while ((x1 != x2) || (y1 != y2)) {
            if (getSachovnice().getFigurka(x1, y1).getColor() != 0) {
                return false;
            }
            x1 += xzm;
            y1 += yzm;
        }
        return true;
    }
}
